package com.powernode.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.dto.PageResult;
import com.powernode.dto.ProductDto;
import com.powernode.dto.Result;
import com.powernode.entity.IndexContent;
import com.powernode.exception.BizException;
import com.powernode.feign.PMSFeign;
import com.powernode.mapper.IndexContentMapper;
import com.powernode.param.IndexContentQueryParam;
import com.powernode.service.IndexContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页内容 服务实现类
 * </p>
 *
 * @author Juice
 * @since 2023-01-28
 */
@Service
public class IndexContentServiceImpl extends ServiceImpl<IndexContentMapper, IndexContent> implements IndexContentService {

    @Autowired
    private PMSFeign PMSFeign;

    @Override
    public PageResult<IndexContent> pageQuery(IndexContentQueryParam queryParam) {
        LambdaQueryWrapper<IndexContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(queryParam.getContentName()), IndexContent::getContentName, queryParam.getContentName());
        wrapper.eq(ObjUtil.isNotNull(queryParam.getContentType()), IndexContent::getContentType, queryParam.getContentType());
        Page<IndexContent> page = baseMapper.selectPage(new Page<>(queryParam.getPageNumber(), queryParam.getPageSize()), wrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public boolean saveIndexContent(IndexContent entity) {
        //TODO 验证商品id，必须在数据库中存在
        if (PMSFeign.getProductById(entity.getProductId()).getData() == null) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "商品id不存在");
        }
        if (!(baseMapper.insert(entity) > 0)) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "新增首页内容失败");
        }
        return true;
    }

    @Override
    public boolean updateIndexContent(IndexContent entity) {
        if (PMSFeign.getProductById(entity.getProductId()).getData() == null) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "商品id不存在");
        }
        if (!(baseMapper.updateById(entity) > 0))
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "首页内容id不存在");
        return true;
    }

    @Override
    public boolean removeIndexContent(Long id) {
        if (!(baseMapper.deleteById(id) > 0))
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "删除首页内容失败");
        return true;
    }

    @Override
    public Result<List<ProductDto>> getContentsForIndex(Integer contentType, Integer num) {
        //指定商品类型获取分页数据并按照排序字段排序
        LambdaQueryWrapper<IndexContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IndexContent::getContentType, contentType);
        wrapper.orderByAsc(IndexContent::getSort);
        Page<IndexContent> page = this.page(new Page<>(1, num), wrapper);
        //获取分页数据中的商品id集合
        List<Long> productIds = new ArrayList<>();
        page.getRecords().forEach(indexContent -> productIds.add(indexContent.getProductId()));
        //远程调用商品微服务获取商品集合
        return PMSFeign.listProductsByIds(productIds);


    }
}
